﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Monkey.Warrenty.Data.Interfaces;
using Monkey.Warrenty.EntityModel;

namespace Monkey.Warrenty.Data
{
    public class SupplierDao : ISupplierDao
    {
        public int GetSupplierCountByFilter(string name, int supplierGroupId)
        {
            using (var context = new MonkeyWarrantyEntities())
            {
                IEnumerable<Supplier> query = context.Suppliers;

                //Filters
                if (!string.IsNullOrEmpty(name))
                {
                    query = query.Where(p => p.Name.IndexOf(name, StringComparison.OrdinalIgnoreCase) >= 0);
                }

                if (supplierGroupId > 0)
                {
                    query = query.Where(p => p.SupplierGroupId == supplierGroupId);
                }

                return query.Count();
            }
        }

        public List<Supplier> GetSuppliersByFilter(string name, int supplierGroupId, int startIndex, int count, string sorting)
        {
            using (var context = new MonkeyWarrantyEntities())
            {
                IEnumerable<Supplier> query = context.Suppliers;

                //Filters
                if (!string.IsNullOrEmpty(name))
                {
                    query = query.Where(c => c.Name.IndexOf(name, StringComparison.OrdinalIgnoreCase) >= 0);
                }

                if (supplierGroupId > 0)
                {
                    query = query.Where(c => c.SupplierGroupId == supplierGroupId);
                }

                //Sorting
                //This ugly code is used just for demonstration.
                //Normally, Incoming sorting text can be directly appended to an SQL query.
                if (string.IsNullOrEmpty(sorting) || sorting.Equals("Name ASC"))
                {
                    query = query.OrderBy(c => c.Name);
                }
                else if (sorting.Equals("Name DESC"))
                {
                    query = query.OrderByDescending(c => c.Name);
                }
                else if (sorting.Equals("Code ASC"))
                {
                    query = query.OrderBy(c => c.Code);
                }
                else if (sorting.Equals("Code DESC"))
                {
                    query = query.OrderByDescending(c => c.Code);
                }
                else if (sorting.Equals("SupplierGroupId ASC"))
                {
                    query = query.OrderBy(c => c.SupplierGroupId);
                }
                else if (sorting.Equals("SupplierGroupId DESC"))
                {
                    query = query.OrderByDescending(c => c.SupplierGroupId);
                }
                else
                {
                    query = query.OrderBy(c => c.Name); //Default!
                }

                return count > 0
                           ? query.Skip(startIndex).Take(count).ToList() //Paging
                           : query.ToList(); //No paging
            }
        }

        public Supplier AddSupplier(Supplier supplier)
        {
            using (var context = new MonkeyWarrantyEntities())
            {
                context.Suppliers.AddObject(supplier);
                context.SaveChanges();
            }

            return supplier;
        }

        public void UpdateSupplier(Supplier supplier)
        {
            if (supplier == null || supplier.Id == 0) return;

            using (var context = new MonkeyWarrantyEntities())
            {
                context.Suppliers.Attach(supplier);
                context.ObjectStateManager.ChangeObjectState(supplier, EntityState.Modified);
                context.SaveChanges();
            }
        }

        public void DeleteSupplier(int supplierId)
        {
            if (supplierId <= 0) return;
            using (var context = new MonkeyWarrantyEntities())
            {
                var supplier = context.Suppliers.First(c => c.Id == supplierId);

                context.DeleteObject(supplier);

                context.SaveChanges();
            }
        }

        public int GetMaxId()
        {
            var maxId = 0;

            using (var context = new MonkeyWarrantyEntities())
            {
                var customerGroup = context.Suppliers.OrderByDescending(x => x.Id).FirstOrDefault();
                if (customerGroup != null) maxId = customerGroup.Id;
            }

            return maxId;
        }

        public int GetCurrentIdentity()
        {
            long? identity;
            using (var context = new MonkeyWarrantyEntities())
            {
                identity = context.GetIdentity("Suppliers").SingleOrDefault();
            }

            return identity.HasValue ? (int)identity.Value : 0;
        }

        public Supplier GetById(int id)
        {
            using (var context = new MonkeyWarrantyEntities())
            {
                return context.Suppliers.FirstOrDefault(x => x.Id == id);
            }
        }

        public List<Supplier> GetAllSuppliers()
        {
            using (var context = new MonkeyWarrantyEntities())
            {
                return context.Suppliers.ToList();
            }
        }
    }
}
